package com.example.demo.auth.service;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.example.demo.auth.dao.LoginInfoDao;
import com.example.demo.core.DefineConstant;
import com.example.demo.entity.LoginEnity;
import com.example.demo.entity.Person;
import com.example.demo.entity.base.ResponseBean;
import com.example.demo.util.EmptyUtil;
import com.example.demo.util.EncryUtil;
import com.example.demo.util.PrimaryKeyUtil;
import com.example.demo.util.ResponseBeanUtil;

@Service
@Transactional
@Deprecated
public class LoginInfoService {
	private Logger logger = LoggerFactory.getLogger(getClass());
	
	@Autowired
	private LoginInfoDao loginInfoDao;
	
	/**
	 * @return
	 * @创建时间 2019年6月26日
	 * @创建人 许子文
	 * @功能描述: 添加登陆用户
	 */
	public ResponseBean addLoginUser(LoginEnity logininfo) {
		
		if(EmptyUtil.isEmpty(logininfo)) {
			return ResponseBeanUtil.error("未填写账号信息");
		}
		
		if(EmptyUtil.isEmpty(logininfo.getLogin_id())) {
			return ResponseBeanUtil.error("未填写登陆账号");
		}
		
		if(EmptyUtil.isEmpty(logininfo.getLogin_password())) {
			return ResponseBeanUtil.error("未填写登陆密码");
		}
		
		if(EmptyUtil.isEmpty(logininfo.getOrgid())) {
			return ResponseBeanUtil.error("未选择归属机构");
		}
		
		List<LoginEnity> checklogin = null;
		
		try {
			checklogin = loginInfoDao.getAllLoginId(logininfo);
		} catch (Exception e) {
			logger.error("验证重复失败",e);
			return ResponseBeanUtil.error("验证重复失败");
		}
		
		if(EmptyUtil.isNotEmpty(checklogin)) {
			return ResponseBeanUtil.error("该账号已注册");
		}
		
		String person_id = PrimaryKeyUtil.getUUIDWithOutLink();
		
		logininfo.setPerson_id(person_id);
		
		Person person = new Person();
		
		person.setPerson_id(person_id);
		
		try {
			loginInfoDao.addPersonInfo(person);
		} catch (Exception e) {
			logger.error("添加个人信息失败",e);
			TransactionAspectSupport.currentTransactionStatus().isRollbackOnly();
			return ResponseBeanUtil.error("添加个人信息失败");
		}
		
		try {
			logininfo.setLogin_password(EncryUtil.encrypt(logininfo.getLogin_password(), DefineConstant.ENCRYKEY));
		} catch (Exception e) {
			logger.error("密码加密失败",e);
			TransactionAspectSupport.currentTransactionStatus().isRollbackOnly();
			return ResponseBeanUtil.error("密码加密失败");
		}
		
		try {
			loginInfoDao.addLoginId(logininfo);
		} catch (Exception e) {
			logger.error("添加账户失败",e);
			TransactionAspectSupport.currentTransactionStatus().isRollbackOnly();
			return ResponseBeanUtil.error("添加账户失败");
		}
		
		return ResponseBeanUtil.success("添加用户成功");
	}
	
	/**
	 * 获取所有的登陆账号信息
	 * @param logininfo 
	 * @return
	 */
	public ResponseBean getAllLoginId() {
		
		List<LoginEnity> loginIds = null;
		
		try {
			loginIds = loginInfoDao.getAllLoginId(null);
		} catch (Exception e) {
			logger.error("查询所有登陆账号失败",e);
			return new ResponseBean(500, "error", "查询所有登陆账号失败");
		}
		
		
		return new ResponseBean(200, "success", loginIds);
	}
}
